package com.qmx.launcher.activity.general;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.RectF;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.open.androidtvwidget.bridge.EffectNoDrawBridge;
import com.open.androidtvwidget.bridge.OpenEffectBridge;
import com.open.androidtvwidget.utils.Utils;
import com.open.androidtvwidget.view.FrameMainLayout;
import com.open.androidtvwidget.view.MainUpView;
import com.qmx.launcher.R;
import com.qmx.launcher.activity.base.BaseActivity;
import com.qmx.launcher.utils.SystemInfoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author ChengXiangtong
 * @create 2018/11/8
 * @Describe 关于
 */
public class AboutActivity extends BaseActivity {
    MainUpView mainUpView1;
    OpenEffectBridge mOpenEffectBridge;
    View mOldFocus; // 4.3以下版本需要自己保存.

    public int requestCode = 0;

    @BindView(R.id.rv_service_tel)
    RelativeLayout rv_service_tel;
    @BindView(R.id.rv_soft_versions)
    RelativeLayout rv_soft_versions;
    @BindView(R.id.rv_system_versions)
    RelativeLayout rv_system_versions;
    @BindView(R.id.rv_hardware_versions)
    RelativeLayout rv_hardware_versions;
    @BindView(R.id.rv_terminal_versions)
    RelativeLayout rv_terminal_versions;
    @BindView(R.id.rv_serial_number)
    RelativeLayout rv_serial_number;
    @BindView(R.id.rv_mac_address)
    RelativeLayout rv_mac_address;
    @BindView(R.id.rv_upload_log)
    RelativeLayout rv_upload_log;
    @BindView(R.id.rv_update)
    RelativeLayout rv_update;
    @BindView(R.id.rv_reset)
    RelativeLayout rv_reset;
    @BindView(R.id.rv_about)
    RelativeLayout rv_about;


    @BindView(R.id.tv_service_tel)
    TextView tv_service_tel;
    @BindView(R.id.tv_soft_versions)
    TextView tv_soft_versions;
    @BindView(R.id.tv_system_versions)
    TextView tv_system_versions;
    @BindView(R.id.tv_hardware_versions)
    TextView tv_hardware_versions;
    @BindView(R.id.tv_terminal_versions)
    TextView tv_terminal_versions;
    @BindView(R.id.tv_serial_number)
    TextView tv_serial_number;
    @BindView(R.id.tv_mac_address)
    TextView tv_mac_address;
//    @BindView(R.id.tv_upload_log)
//    RelativeLayout tv_upload_log;
//    @BindView(R.id.tv_update)
//    RelativeLayout tv_update;
//    @BindView(R.id.tv_reset)
//    RelativeLayout tv_reset;
//    @BindView(R.id.tv_about)
//    RelativeLayout tv_about;

    private Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        ButterKnife.bind(this);
        context = this;
        /* MainUpView 设置. */
        mainUpView1 = (MainUpView) findViewById(R.id.mainUpView1);
        mOpenEffectBridge = (OpenEffectBridge) mainUpView1.getEffectBridge();
        if (Utils.getSDKVersion() == 17) { // 测试 android 4.2版本.
            switchNoDrawBridgeVersion();
        } else { // 其它版本（android 4.3以上）.
            mainUpView1.setUpRectResource(R.drawable.test_rectangle); // 设置移动边框的图片.
            mainUpView1.setShadowResource(R.drawable.item_shadow); // 设置移动边框的阴影.
        }
        // mainUpView1.setUpRectResource(R.drawable.item_highlight); //
        // 设置移动边框的图片.(test)
        // mainUpView1.setDrawUpRectPadding(new Rect(0, 0, 0, -26)); //
        // 设置移动边框的距离.
        // mainUpView1.setDrawShadowPadding(0); // 阴影图片设置距离.
        // mOpenEffectBridge.setTranDurAnimTime(500); // 动画时间.

        FrameMainLayout main_lay = (FrameMainLayout)findViewById(R.id.main_lay);
        main_lay.getViewTreeObserver().addOnGlobalFocusChangeListener(new ViewTreeObserver.OnGlobalFocusChangeListener() {
            @Override
            public void onGlobalFocusChanged(final View oldFocus, final View newFocus) {
                if (newFocus != null)
                    newFocus.bringToFront(); // 防止放大的view被压在下面. (建议使用MainLayout)
                float scale = 1.01f;
                mainUpView1.setFocusView(newFocus, mOldFocus, scale);
                mOldFocus = newFocus; // 4.3以下需要自己保存.
                // 测试是否让边框绘制在下面，还是上面. (建议不要使用此函数)
                if (newFocus != null) {
//                    testTopDemo(newFocus, scale);
                }
            }
        });

        initText();
    }


    public void initText() {
        tv_soft_versions.setText(SystemInfoUtils.getAppVersionCode(context) + "");
        tv_system_versions.setText(SystemInfoUtils.getOSVersionCode() + "");
        tv_hardware_versions.setText(SystemInfoUtils.getBrandName());
        tv_terminal_versions.setText(SystemInfoUtils.getModelName());
        tv_serial_number.setText("12345678");
        tv_mac_address.setText(SystemInfoUtils.getMac(context));
    }


    @OnClick({R.id.rv_upload_log, R.id.rv_update, R.id.rv_reset, R.id.rv_about})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rv_upload_log:
                uploadLog();
                break;
            case R.id.rv_update:
                update();
                break;
            case R.id.rv_reset:
                startFactoryDefault(AboutActivity.this);
                break;
            case R.id.rv_about:
                aboutUs();
                break;
        }
    }


    public void uploadLog() {
        Toast.makeText(context, "日志上传中......", Toast.LENGTH_SHORT).show();
    }

    public void update() {
        Toast.makeText(context, "版本更新中......", Toast.LENGTH_SHORT).show();
    }

    public void aboutUs() {
        startActivity(new Intent(context, AboutUsActivity.class));
    }

    private void switchNoDrawBridgeVersion() {
        float density = getResources().getDisplayMetrics().density;
        RectF rectf = new RectF(getDimension(R.dimen.w_10) * density, getDimension(R.dimen.h_10) * density,
                getDimension(R.dimen.w_9) * density, getDimension(R.dimen.h_9) * density);
        EffectNoDrawBridge effectNoDrawBridge = new EffectNoDrawBridge();
        effectNoDrawBridge.setTranDurAnimTime(200);
//        effectNoDrawBridge.setDrawUpRectPadding(rectf);
        mainUpView1.setEffectBridge(effectNoDrawBridge); // 4.3以下版本边框移动.
        mainUpView1.setUpRectResource(R.drawable.white_light_10); // 设置移动边框的图片.
        mainUpView1.setDrawUpRectPadding(rectf); // 边框图片设置间距.

    }

    public float getDimension(int id) {
        return getResources().getDimension(id);
    }


    public static void startFactoryDefault(Context context) {
        if (Build.VERSION.SDK_INT < 26) {
            context.sendBroadcast(new Intent("android.intent.action.MASTER_CLEAR"));
        } else {
            Intent intent = new Intent("android.intent.action.FACTORY_RESET");
            intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            intent.setPackage("android");
            context.sendBroadcast(intent);
        }
    }
}
